package com.zto.demo.boot.rocketmq;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import com.alibaba.rocketmq.client.exception.MQClientException;
import com.alibaba.rocketmq.client.producer.DefaultMQProducer;

@Component
@ConfigurationProperties(prefix = "rocketmq")
public class RocketMqProducer {

	private DefaultMQProducer producer;
    private String producerGroup;
    private String instanceName;
    private String namesrvAddr;
    
    @PostConstruct
    public DefaultMQProducer mqProducer() throws MQClientException {
    	System.out.println("boot Producer start....");
    	producer = new DefaultMQProducer(producerGroup);
        producer.setNamesrvAddr(namesrvAddr);
        producer.setInstanceName(instanceName);
        producer.start();
        return producer;
    }
    
    /**
	 * Spring bean destroy-method
	 */
    @PreDestroy
	public void destroy() {
		System.out.println("boot Producer stop....");
		producer.shutdown();
	}
    
    public DefaultMQProducer getDefaultMQProducer() {
		return producer;
	}
 
    public String getProducerGroup() {
        return producerGroup;
    }
 
    public void setProducerGroup(String producerGroup) {
        this.producerGroup = producerGroup;
    }
 
    public String getInstanceName() {
        return instanceName;
    }
 
    public void setInstanceName(String instanceName) {
        this.instanceName = instanceName;
    }

	public String getNamesrvAddr() {
		return namesrvAddr;
	}

	public void setNamesrvAddr(String namesrvAddr) {
		this.namesrvAddr = namesrvAddr;
	}
    
}
